<#include "../common/page.ftl" />
<#include "../common/head.ftl" />
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>用户角色关系列表</title>
    <@comme_head />
    <link rel="stylesheet" href="/css/user-role.css" type="text/css">
    <script type="text/javascript">
	    $(function() {
	    	$(".partCheck").removeAttr("checked");
	    	$(".userCheck").removeAttr("checked");
	    });
    
    	//点击用户事件
    	function clickUser(id){
    		$(".partCheck").removeAttr("checked");
    		//console.log($("#pars").find("input:checkbox"))
    		$.ajax({
    			   type: "GET",
				   url: "/manager/getPartByUser",
				   data: "id="+id,
				   success: function(msg){
				   		var msg1=eval(msg);
						for(i=0;i<msg1.length;i++){
							$("#part_"+msg1[i].partId).attr("checked","checked");
						}
				   }
    		});
    	
    	}
    	
    	//用户多选框
    	function usersChange(a){
    		$(".userCheck").removeAttr("checked");
    		$("#user_"+a).attr("checked","checked");
    	}
    	
    	
    	//角色多选框
    	function partsChange(a){
    	
    		if($(".userCheck[checked='checked']").length<1){
    			alert("未选择用户！");
    			$("#part_"+a).removeAttr('checked');
    			return;
    		}
    	
    		//添加角色
    		if($("#part_"+a).attr('checked')){
    			if(!confirm("确定要赋予此角色权限吗？")){
    				$("#part_"+a).removeAttr('checked');
    				return;
    			}
    			$.ajax({
				   type: "GET",
				   url: "/manager/add_UserByPart",
				   data: "user_id="+$(".userCheck[checked]").val()+"&part_id="+$("#part_"+a).val(),
				   success: function(msg){
				     alert( "设置成功!");
				   }
				});
    		}else{//取消角色
    			if(!confirm("确定要取消此角色权限吗？")){
    				$("#part_"+a).attr('checked','checked');
					return;    			
    			}
    			$.ajax({
				   type: "GET",
				   url: "/manager/removeUserPart",
				   data: "user_id="+$(".userCheck[checked]").val()+"&part_id="+$("#part_"+a).val(),
				   success: function(msg){
				   		$("#part_"+a).removeAttr('checked');
				    	alert( "设置成功!");
				   }
				});
    		}
    	}
    </script>
</head>
<body>
    <@comme_header />
    <div class="main clearfix" id="main">
    	<@comme_daohang />
        <div class="right-content">
            <div class="content">
                <div class="yonghu">
                    <ul class="yh-left">
                        <p>用户列表</p>
                        <#list data as being>
						   <li onclick="clickUser(${being.id})"> <label><input type="checkbox" id="user_${being.id}" onChange="usersChange(${being.id})" class="userCheck" value="${being.id}">${being.name} </label></li>
						</#list>
                    </ul>
                    <div style="position: absolute;left: 314px;bottom: 38px;">
						<@page pageid="" url="/manager/userspart_querylist?currentpageno=" />
					</div>
                    <div class="yh-right">
                        <p class="js-title">角色列表</p>
                        <p class="all-check"><label><input type="checkbox"/><span>全选</span></label></p>
                        <div class="check-nr clearfix" id="pars">
                        	<#list partList as part>
                        		 <label><input type="checkbox" onChange="partsChange(${part.id})" value="${part.id}" id="part_${part.id}" class="partCheck"/>${part.name}</label>
                        	</#list>
                        </div>
                    </div>
                    <div class="clear"></div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>